package com.hhit.bigscreenbackmanage.controller;


import com.hhit.bigscreenbackmanage.common.Response;
import com.hhit.bigscreenbackmanage.entity.tgygl.TgyglFileEntity;
import com.hhit.bigscreenbackmanage.service.ImportService;
import com.hhit.bigscreenbackmanage.service.gis.TGisSituationService;
import com.hhit.bigscreenbackmanage.service.impl.jjgl.JjglImportServiceImpl;
import com.hhit.bigscreenbackmanage.service.jsgl.*;
import com.hhit.bigscreenbackmanage.service.tgygl.*;
import com.hhit.bigscreenbackmanage.service.jjgl.TJjglWgglMxService;
import com.hhit.bigscreenbackmanage.service.zlgl.TZlglActualMeasurementService;
import com.hhit.bigscreenbackmanage.service.zlgl.TZlglSbdlsdtyzsfgcsbxmService;
import org.seimicrawler.xpath.JXDocument;
import org.seimicrawler.xpath.JXNode;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * 导入接口
 * 2021/7/20 15:20
 * caojie
 */
@RestController
@RequestMapping("/importController")
public class importController {

    private static Logger log = LoggerFactory.getLogger(importController.class);

    @Autowired
    JjglImportServiceImpl jjglImportServiceImpl;

    @Autowired
    TJsglCspsxmService tJsglCspsxmService;

    @Autowired
    TJsglSjzlwtService tJsglSjzlwtService;

    @Autowired
    TJsglTysjBdzService tJsglTysjBdzService;

    @Autowired
    TJsglSzjjService tJsglSzjjService;

    @Autowired
    TJsglStrwjdService tJsglStrwjdService;

    @Autowired
    TZlglSbdlsdtyzsfgcsbxmService tZlglSbdlsdtyzsfgcsbxmService;

    @Autowired
    TgyglService tgyglService;

    @Autowired
    TJsglTysbyyService tJsglTysbyyService;

    @Autowired
    TTgyglConverterStationService tTgyglConverterStationService;

    @Autowired
    TTgyglLicenseProcessingService tTgyglLicenseProcessingService;
    @Autowired
    TTgyglProgressComparisonService tgyglProgressComparisonService;
    @Autowired
    TTgyglEngineeringPlanActualSpeedService tTgyglEngineeringPlanActualSpeedService;
    @Autowired
    TTgyglProgressComparisonYcService tTgyglProgressComparisonYcService;
    @Autowired
    TTgyglMonthBuildPlanPtscService tTgyglMonthBuildPlanPtscService;
    @Autowired
    TTgyglMonthBuildPlanService tTgyglMonthBuildPlanService;
    @Autowired
    TTgyglLicenseProcessingPlanService tTgyglLicenseProcessingPlanService;
    @Autowired
    TTgyglConstructPlanAmiYcService tTgyglConstructPlanAmiYcService;

    @Autowired
    TZlglActualMeasurementService tZlglActualMeasurementService;

    @Autowired
    TTgyglGcjzService tgyglGcjzService;

    @Autowired
    TTgyglGzdpService tTgyglGzdpService;

    @Autowired
    TTgyglGlgzService tTgyglGlgzService;

    @Autowired
    TTgyglZdgzService tTgyglZdgzService;

    @Autowired
    TGisSituationService tGisSituationService;

    @Autowired
    TJjglWgglMxService TJjglWgglMxService;

    @Autowired
    ImportService importService;


    @PostMapping("/importExcel")
    public Response importExcel(@RequestParam("file") MultipartFile[] file,
                                @RequestParam("propName") String propName,
                                @RequestParam("dataTime") String dataTime,
                                @RequestParam(name = "flagParam", required = false) String flagParam) {
        Map<String, Object> stringObjectMap = new HashMap<>();
        try {
            if ("ysglssqkybColumnName".equals(propName)) {
                stringObjectMap = jjglImportServiceImpl.importSgtYsglssqkybData(propName, file, dataTime);
            }
            if ("cspsybColumnName".equals(propName)) {
                stringObjectMap = jjglImportServiceImpl.importCspsyb(propName, file, dataTime);
            }
            if ("fbjsfbColumnName".equals(propName)) {
                stringObjectMap = jjglImportServiceImpl.importFbjsfb(propName, file, dataTime);
            }
            if ("jsxmjsgsjeColumnName".equals(propName)) {
                stringObjectMap = jjglImportServiceImpl.importJsxmJsgsje(propName, file, dataTime);
            }
            if ("tcxmwggcglgzbbqgbxmColumnName".equals(propName)) {
                stringObjectMap = jjglImportServiceImpl.importTcxmWggcglgzbBqgbxm(propName, file, dataTime);
            }
            if ("tcxmwggcglgzbtcxmColumnName".equals(propName)) {
                stringObjectMap = jjglImportServiceImpl.importTcxmWggcglgzbTcxm(propName, file, dataTime);
            }
            if ("bdzTysjColumnName".equals(propName) || "bdzTcqkColumnName".equals(propName)) {
                stringObjectMap = tJsglTysjBdzService.insertTJsglTysjBdz(file, dataTime, propName);
            }
            if ("sjzlwtColumnName".equals(propName)) {
                stringObjectMap = tJsglSjzlwtService.insertTJsglSjzlwt(file, dataTime, propName);
            }
            if ("sjzlwtSjbgColumnName".equals(propName)) {
                stringObjectMap = tJsglSjzlwtService.insertTJsglSjzlwtSjbg(file, dataTime, propName);
            }
            if ("sjzlwtZlsjColumnName".equals(propName)) {
                stringObjectMap = tJsglSjzlwtService.insertTJsglSjzlwtZlsj(file, dataTime, propName);
            }
            if ("sjzlwtYttbColumnName".equals(propName)) {
                stringObjectMap = tJsglSjzlwtService.insertTJsglSjzlwtYttb(file, dataTime, propName);
            }
            if ("cspsxmColumnName".equals(propName)) {
                stringObjectMap = tJsglCspsxmService.insertTJsglCspsxm(file, dataTime, propName);
            }
            if ("gdzyszxqkColumnName".equals(propName)) {
                stringObjectMap = jjglImportServiceImpl.importYszxqk(propName, file, dataTime);
            }
            if ("gcgsJsjshzbColumnName".equals(propName)) {
                stringObjectMap = jjglImportServiceImpl.importGcgsJsjshzb(propName, file, dataTime);
            }
            if ("jjzdgzydxjColumnName".equals(propName)) {
                stringObjectMap = jjglImportServiceImpl.importJjzdgzydxj(propName, file, dataTime);
            }
            if ("lsdtyzsfgcColumnName".equals(propName)) {
                stringObjectMap = tZlglSbdlsdtyzsfgcsbxmService.insertTZlglSbdlsdtyzsfgcsbxm(file, dataTime, propName);
            }
            if ("scslColumnName".equals(propName) || "scslsbColumnName".equals(propName) || "scslzjColumnName".equals(propName)) {
                stringObjectMap = tZlglActualMeasurementService.insertTZlglActualMeasurement(file, dataTime, propName);
            }
            // 尾工项目情况
            if ("wgxmqkColumnName".equals(propName)) {
                stringObjectMap = jjglImportServiceImpl.importWgxmqk(propName, file, dataTime);
            }
            // 尾工管理明细
            if ("wgglmxColumnName".equals(propName)) {
                stringObjectMap = TJjglWgglMxService.importWgglMx(propName, file, dataTime);
            }
            //gis数据导入
            if ("gisColumnName".equals(propName)) {
                if (flagParam != null) {
                    if (Integer.valueOf(flagParam) == 1) {
                        stringObjectMap = tGisSituationService.insertFromExcelDfm(file, dataTime, propName);
                    }
                } else {
                    stringObjectMap = tGisSituationService.insertFromExcel(file, dataTime, propName);

                }

            }
            // 农民工工资汇总
            if ("nmggzhzColumnName".equals(propName)) {
                stringObjectMap = jjglImportServiceImpl.importNmggzHz(propName, file, dataTime);
            }
            if ("strwjdColumnName".equals(propName)) {
                stringObjectMap = tJsglStrwjdService.insertTJsglStrwjd(file, dataTime, propName);
            }
            if ("szjjColumnName".equals(propName)) {
                stringObjectMap = tJsglSzjjService.insertTJsglSzjj(file, dataTime, propName);
            }
            if ("tysbyyColumnName".equals(propName)) {
                stringObjectMap = tJsglTysbyyService.insertTJsglTysbyy(file, dataTime, propName);
            }
        } catch (Exception e) {
            log.error("导入失败: " + e.getMessage());
        }
        Boolean flag = (Boolean) stringObjectMap.get("flag");
        if (flag) {
            return Response.createResponseBySuccess("导入成功", null);
        } else {
            return Response.createResponseByError("导入失败:" + null);
        }
    }

    /**
     * word文件导入、解析、入库
     *
     * @param file
     * @param propName
     * @param htmlStr
     * @return
     */
    @PostMapping("/importWord")
    public Response importWord(@RequestParam("file") MultipartFile[] file,
                               @RequestParam("propName") String propName,
                               @RequestParam("htmlStr") String htmlStr,
                               @RequestParam("nf") String nf,
                               @RequestParam("yf") String yf) {
        Map<String, Object> stringObjectMap = new HashMap<>();
        Map<String, Object> resultMap = new HashMap<>();
        Boolean flag = false;
        if ("tgy".equals(propName)) { //特高压
            JXDocument underTest = JXDocument.create(htmlStr);
            System.out.println(underTest);
            List<JXNode> jxNodeList = underTest.selN("//body");
            JXNode jxNode = jxNodeList.get(0);
            List<JXNode> pjxNodeList = jxNode.sel("//p//text()");
            List<JXNode> tablejxNodeList = jxNode.sel("//table");
            JXNode jxNode1 = tablejxNodeList.get(0);
            try {
                stringObjectMap = tgyglService.uploadFile(file);
                if (stringObjectMap.get("flag").equals(true)) { //导入成功
                    TgyglFileEntity tgyglFileEntity = tgyglService.initTitles(pjxNodeList);
                    tgyglFileEntity.setFileName(stringObjectMap.get("fileName").toString());
                    tgyglFileEntity.setFileUri(stringObjectMap.get("fileUri").toString());
                    tgyglFileEntity.setNf(nf);
                    tgyglFileEntity.setYf(yf);
                    tgyglFileEntity.setHtmlContent(htmlStr);
                    resultMap = tgyglService.insertFile(tgyglFileEntity);
                }
            } catch (Exception e) {
                //手动开启事务回滚
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                e.printStackTrace();
            }
            flag = (Boolean) resultMap.get("flag");
            //文件Id
            int insertRecordId = Integer.parseInt(resultMap.get("id").toString());
            // 插入工程进展
            try {
                tgyglGcjzService.insert(pjxNodeList, insertRecordId);
            } catch (Exception e) {
                e.printStackTrace();
            }

            // 插入工作点评
            try {
                tTgyglGzdpService.insert(pjxNodeList, insertRecordId);
            } catch (Exception e) {
                e.printStackTrace();
            }

            // 插入管理工作
            try {
                tTgyglGlgzService.insert(pjxNodeList, insertRecordId);
            } catch (Exception e) {
                e.printStackTrace();
            }

            // 插入重点工作
            try {
                tTgyglZdgzService.insert(pjxNodeList, insertRecordId);
            } catch (Exception e) {
                e.printStackTrace();
            }

            // 插入table
            try {
                // 附件1: 各属地公司月份政处及证照办理情况
                tTgyglLicenseProcessingService.insertWord(tablejxNodeList.get(0), insertRecordId, Integer.parseInt(nf), Integer.parseInt(yf));

                //附件2.1：白江线路工程全线进度对比表（截至6月30日）
                tgyglProgressComparisonService.insertWord(tablejxNodeList.get(1), insertRecordId, Integer.parseInt(nf), Integer.parseInt(yf));

                //附件2.2: 江苏段线路工程计划进度与实际进度对比表（截至6月30日） t_tgygl_engineering_plan_actual_speed
                tTgyglEngineeringPlanActualSpeedService.insertWord(tablejxNodeList.get(2), insertRecordId, Integer.parseInt(nf), Integer.parseInt(yf));

                // 附件2.3：送、受换流站工程进度对比表
                tTgyglConverterStationService.insert(tablejxNodeList.get(3), insertRecordId);

                //附件2.4: 虞城换流站工程计划进度与实际进度对比表（截至6月30日）
                tTgyglProgressComparisonYcService.insertWord(tablejxNodeList.get(4), insertRecordId, Integer.parseInt(nf), Integer.parseInt(yf));

                //附件3: 各属地公司7月份政处及证照办理任务目标
                tTgyglLicenseProcessingPlanService.insertWord(tablejxNodeList.get(5), insertRecordId, Integer.parseInt(nf), Integer.parseInt(yf));
                //附件4.1: 虞城换流站工程7月份建设进度目标
                tTgyglConstructPlanAmiYcService.insertWord(tablejxNodeList.get(6), insertRecordId, Integer.parseInt(nf), Integer.parseInt(yf));

                //附件4.2：江苏段线路工程7月份建设进度目标
                tTgyglMonthBuildPlanService.insertWord(tablejxNodeList.get(7), insertRecordId, Integer.parseInt(nf), Integer.parseInt(yf));

                //附件4.3：配套送出工程7月份建设进度目标
                tTgyglMonthBuildPlanPtscService.insertWord(tablejxNodeList.get(8), insertRecordId, Integer.parseInt(nf), Integer.parseInt(yf));

            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        if (flag) {
            return Response.createResponseBySuccess("导入成功", resultMap);
        } else {
            return Response.createResponseByError("导入失败:" + null);
        }
    }


    @PostMapping("/importExcelNdJh")
    public Response importExcelNdJh(@RequestParam("file") MultipartFile[] file, @RequestParam("propName") String propName, @RequestParam("jsgldw") String jsgldw) {
        Map<String, Object> stringObjectMap = jjglImportServiceImpl.importExcelNdJh(propName, file, jsgldw);
        Boolean flag = (Boolean) stringObjectMap.get("flag");
        if (flag) {
            return Response.createResponseBySuccess("导入成功", null);
        } else {
            return Response.createResponseByError("导入失败:" + null);
        }
    }

    @PostMapping("/importExcelNdJh2")
    public Response importExcelNdJh2(@RequestParam("file") MultipartFile[] file, @RequestParam("propName") String propName) {
        Map<String, Object> stringObjectMap = jjglImportServiceImpl.importExcelNdJh2(propName, file);
        Boolean flag = (Boolean) stringObjectMap.get("flag");
        if (flag) {
            return Response.createResponseBySuccess("导入成功", null);
        } else {
            return Response.createResponseByError("导入失败:" + null);
        }
    }

    /**
     * 每月读取所有数据
     * @return
     */
    @GetMapping("/importDataEveryMonth")
    public Response importDataEveryMonth() throws Exception{
        Map<String, Object> map = importService.importDataEveryMonth();
        Boolean flag = (Boolean) map.get("flag");
        if (flag) {
            return Response.createResponseBySuccess("导入成功", null);
        } else {
            return Response.createResponseByError("导入失败:" + null);
        }
    }

    /**
     * 检测是否存在新文件
     * @return
     * @throws Exception
     */
    @GetMapping("/checkDataEveryMonth")
    public Map<String, Object> checkDataEveryMonth(@RequestParam("param") String param) throws Exception{
        Map result = importService.checkDataEveryMonth(param);
        return result;
    }
}